import { http } from "@/utils/http";
import baseUrlProxy from "@/api/baseUrlProxy";
import type { Dept, Job, Role } from "@/api/user";
import qs from "qs";
export type ResultDict = {
  content: DictItem[];
  totalElements: number;
};
export type DictListItem = {
  createTime: string;
  description: string;
  dictDetails: DictItem[];
  name: string;
  id: number;
};
export type ResultDictList = {
  content: DictListItem[];
  totalElements: number;
};
export type DictId = {
  id: string;
};
export type DictItem = {
  createTime: string;
  dict: DictId;
  dictSort: number;
  id: string;
  label: string;
  value: string;
};

export const getDictData = (data?: object) => {
  return http.request<ResultDict>("get", baseUrlProxy + "/api/dictDetail", {
    params: data
  });
};

export const addDictData = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/dictDetail", {
    data
  });
};
export const editDictData = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/dictDetail", {
    data
  });
};
export const delDictData = id => {
  return http.request<any>("delete", baseUrlProxy + "/api/dictDetail/" + id);
};
export const getDictList = (data?: object) => {
  return http.request<ResultDictList>("get", baseUrlProxy + "/api/dict", {
    params: data
  });
};
export const getDictListAll = (data?: object) => {
  return http.request<DictListItem[]>("get", baseUrlProxy + "/api/dict/all", {
    params: data
  });
};
export const addDictListApi = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/dict", {
    data
  });
};
export const editDictListApi = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/dict", {
    data
  });
};
export const delDictListApi = (data?: object) => {
  return http.request<any>("delete", baseUrlProxy + "/api/dict", {
    data
  });
};

export const exportDictList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/dict/download",
    { params: data },
    { responseType: "blob" }
  );
};
export type DeptItem = {
  createBy: string;
  createTime: string;
  deptSort: number;
  enabled: boolean;
  hasChildren: boolean;
  id: number;
  label: string;
  leaf: boolean;
  name: string;
  subCount: number;
  updateBy: string;
  updateTime: string;
};
export type ResultDept = {
  content: DeptItem[];
  totalElements: number;
};
export const getDeptList = (data?: object) => {
  return http.request<ResultDept>("get", baseUrlProxy + "/api/dept", {
    params: data,
    paramsSerializer: function (params: any) {
      return qs.stringify(params, { arrayFormat: "repeat" });
    }
  });
};
export const editDeptApi = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/dept", {
    data
  });
};
export const addDeptApi = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/dept", {
    data
  });
};
export const delDeptApi = (data?: object) => {
  return http.request<any>("delete", baseUrlProxy + "/api/dept", {
    data
  });
};

export type UserItem = {
  avatarPath: string;
  createBy: string;
  createTime: string;
  email: string;
  enabled: boolean;
  gender: string;
  id: number;
  nickName: string;
  phone: string;
  updateBy: string;
  updateTime: string;
  username: string;
  dept: Dept;
  jobs: Job[];
  roles: Role[];
};
export type ResultUser = {
  content: UserItem[];
  totalElements: number;
};

export const getUserList = (data?: object) => {
  return http.request<ResultUser>("get", baseUrlProxy + "/api/users", {
    params: data,
    paramsSerializer: function (params: any) {
      return qs.stringify(params, { arrayFormat: "repeat" });
    }
  });
};

export type JobItem = {
  createTime: string;
  enabled: boolean;
  id: number;
  jobSort: number;
  name: string;
};
export type ResultJob = {
  content: JobItem[];
  totalElements: number;
};

export const getJobList = (data?: object) => {
  return http.request<ResultJob>("get", baseUrlProxy + "/api/job", {
    params: data,
    paramsSerializer: function (params: any) {
      return qs.stringify(params, { arrayFormat: "repeat" });
    }
  });
};
export const addJobApi = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/job", {
    data
  });
};
export const editJobApi = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/job", {
    data
  });
};
export const delJobApi = (data?: object) => {
  return http.request<any>("delete", baseUrlProxy + "/api/job", {
    data
  });
};
export const exportJobList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/job/download",
    {
      params: data,
      paramsSerializer: function (params: any) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      }
    },
    { responseType: "blob" }
  );
};
export type MenuItem = {
  cache: boolean;
  component: string;
  componentName: string;
  createTime: string;
  hasChildren: boolean;
  hidden: boolean;
  iFrame: boolean;
  icon: string;
  id: number;
  label: string;
  leaf: boolean;
  menuSort: number;
  path: string;
  permission: string;
  pid: number;
  subCount: number;
  title: string;
  type: number;
};
export type RoleItem = {
  createTime: string;
  dataScope: string;
  depts: Dept[];
  description: string;
  id: number;
  level: number;
  name: string;
  updateBy: string;
  updateTime: string;
  menus: MenuItem[];
};

export const getRoleList = (data?: object) => {
  return http.request<RoleItem[]>("get", baseUrlProxy + "/api/roles/all", {
    params: data
  });
};

export type ResultLevel = {
  level: number;
};

export const getCurrentLevel = (data?: object) => {
  return http.request<ResultLevel[]>("get", baseUrlProxy + "/api/roles/level", {
    params: data
  });
};
export const exportUserList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/users/download",
    {
      params: data,
      paramsSerializer: function (params: any) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      }
    },
    { responseType: "blob" }
  );
};
export const exportRoleList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/roles/download",
    {
      params: data,
      paramsSerializer: function (params: any) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      }
    },
    { responseType: "blob" }
  );
};

export const addUserApi = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/users", { data });
};
export const editUserApi = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/users", { data });
};
export const resetPwdApi = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/users/resetPwd", {
    data
  });
};
export const delUserApi = (data?: object) => {
  return http.request<any>("delete", baseUrlProxy + "/api/users", { data });
};
export const getDeptFilter = (data?: object, params?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/dept/superior", {
    data: data,
    params: params
  });
};
export function getDeptSuperior(ids, exclude?) {
  exclude = exclude !== undefined ? exclude : false;
  const data = ids.length || ids.length === 0 ? ids : Array.of(ids);
  return http.request(
    "post",
    baseUrlProxy + "/api/dept/superior?exclude=" + exclude,
    { data }
  );
}
export type ResultRole = {
  content: RoleItem[];
  totalElements: number;
};
export const getRoleListByPage = (data?: object) => {
  return http.request<ResultRole>("get", baseUrlProxy + "/api/roles", {
    params: data,
    paramsSerializer: function (params: any) {
      return qs.stringify(params, { arrayFormat: "repeat" });
    }
  });
};
export const addRole = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/roles", { data });
};
export const DelRole = (data?: object) => {
  return http.request<any>("delete", baseUrlProxy + "/api/roles", { data });
};
export const editRole = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/roles", { data });
};
export const getMenuSubById = (data?: object) => {
  return http.request<number[]>("get", baseUrlProxy + "/api/menus/child", {
    params: data
  });
};
export type ResultMenu = {
  content: MenuItem[];
  totalElements: number;
};
export const getMenuList = (data?: object) => {
  return http.request<ResultMenu[]>("get", baseUrlProxy + "/api/menus", {
    params: data,
    paramsSerializer: function (params: any) {
      return qs.stringify(params, { arrayFormat: "repeat" });
    }
  });
};
export const addMenuApi = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/menus", {
    data
  });
};
export const editMenuApi = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/menus", {
    data
  });
};
export const delMenuApi = (data?: object) => {
  return http.request<any>("delete", baseUrlProxy + "/api/menus", {
    data
  });
};
export const getMenuListLazy = (data?: object) => {
  return http.request<MenuItem[]>("get", baseUrlProxy + "/api/menus/lazy", {
    params: data
  });
};
export const exportMenuList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/menus/download",
    {
      params: data,
      paramsSerializer: function (params: any) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      }
    },
    { responseType: "blob" }
  );
};
export const exportDeptList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/dept/download",
    {
      params: data,
      paramsSerializer: function (params: any) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      }
    },
    { responseType: "blob" }
  );
};
export const getSuperior = (data?: object) => {
  return http.request<MenuItem[]>(
    "post",
    baseUrlProxy + "/api/menus/superior",
    {
      data
    }
  );
};

export const updateRoleMenu = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/roles/menu", { data });
};

export const getRoleById = id => {
  return http.request<RoleItem>("get", baseUrlProxy + "/api/roles/" + id);
};

export type JobsItem = {
  beanName: string;
  createBy: string;
  createTime: string;
  cronExpression: string;
  description: string;
  jobName: string;
  methodName: string;
  personInCharge: string;
  updateBy: string;
  updateTime: string;
  pauseAfterFailure: string;
  isPause: boolean;
  id: number;
};
export type ResultJobs = {
  content: JobsItem[];
  totalElements: number;
};
export const getJobsList = (data?: object) => {
  return http.request<ResultJobs>("get", baseUrlProxy + "/api/jobs", {
    params: data,
    paramsSerializer: function (params: any) {
      return qs.stringify(params, { arrayFormat: "repeat" });
    }
  });
};

export type JobsLogItem = {
  beanName: string;
  createTime: string;
  cronExpression: string;
  exceptionDetail: string;
  jobName: string;
  methodName: string;
  time: number;
  isSuccess: boolean;
  id: number;
};

export type ResultJobSLog = {
  content: JobsLogItem[];
  totalElements: number;
};

export const getJobsLogList = (data?: object) => {
  return http.request<ResultJobSLog>("get", baseUrlProxy + "/api/jobs/logs", {
    params: data,
    paramsSerializer: function (params: any) {
      return qs.stringify(params, { arrayFormat: "repeat" });
    }
  });
};
export const exportJobsLogList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/jobs/logs/download",
    {
      params: data,
      paramsSerializer: function (params: any) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      }
    },
    { responseType: "blob" }
  );
};
export const addJobsListApi = (data?: object) => {
  return http.request<any>("post", baseUrlProxy + "/api/jobs", {
    data
  });
};

export const editJobsListApi = (data?: object) => {
  return http.request<any>("put", baseUrlProxy + "/api/jobs", {
    data
  });
};
export const updateIsPause = id => {
  return http.request<any>("put", baseUrlProxy + "/api/jobs/" + id);
};
export const execution = id => {
  return http.request<any>("put", baseUrlProxy + "/api/jobs/exec/" + id);
};

export const delJobsListApi = (data?: object) => {
  return http.request<any>("delete", baseUrlProxy + "/api/jobs", {
    data
  });
};

export const exportJobsList = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlProxy + "/api/jobs/download",
    {
      params: data,
      paramsSerializer: function (params: any) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      }
    },
    { responseType: "blob" }
  );
};
/** 获取系统监控-系统日志列表 */
export const getSystemLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/system-logs", { data });
};
type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};
